<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Factory;
use think\Db;

use think\Config;
use think\Validate;//验证
use fast\Random;
use fast\Http;

use app\api\controller\Invite;



/**
 * 老师🏚
 */
class Teacher extends Invite
{

    protected $noNeedLogin = ['help'];
    protected $noNeedRight = ['*'];

    protected $db = 'teacher';

    public function _initialize()
    {
        parent::_initialize();

    }

    /**
     * 是否同意审核
     *
     * @return boolean
     */
    protected function is_agree()
    {
        if(!$this->user['com']['status_switch']){
            $this->error('暂未通过审核哦');
        }
    }

    /**
     * 说明文档
     *
     * @return void
     */
    public function help()
    {
        $this->success('老师角色才可以调此接口');
    }

    /**
     * 获取申请信息
     *
     * @return void
     */
    public function get_apply_data()
    {
        $this->success('ok',Db::name($this->db)->where('uid',$this->auth->id)->find());
    }



    /**
     * 修改申请信息
     *
     * @param string $name 姓名
     * @param string $wechat 微信号
     * @param string $ltd 公司
     * @param string $email 邮箱
     * @param string $tel 电话
     * @param string $inviter 邀请人
     * @param string $ps 备注
     * @return void
     */ 
    public function update_apply()
    {   
        $data = Db::name($this->db)->where('uid',$this->auth->id)->find();
        if(!$data){
            $this->error('你还没申请呢');
        }
        // if($data['status_switch']==1){
        //     $this->error('已通过不能修改了呢');
        // }

        $data['name'] = $this->request->param('name');
        $data['wechat'] = $this->request->param('wechat');
        $data['ltd'] = $this->request->param('ltd');
        $data['email'] = $this->request->param('email');
        $data['tel'] = $this->request->param('tel');
        $data['inviter'] = $this->request->param('inviter');    
        $data['ps'] = $this->request->param('ps');       


        $u['uid'] = $this->auth->id;


        $res = Db::name($this->db)->where($u)->update($data);
        if($res){
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

    /**
     * 获取我的提成列表
     *
     * @param int $page 1
     * @param int $limit 10
     * @return void
     */
    public function get_commission_list($page=1,$limit=10)
    {
        $this->is_agree();
        $list = Db::name('commission')->where('uid',$this->auth->id)->page($page,$limit)->select();
        $this->success('ok',$list);
        // if(Config::get('look_commission')){
        //     $list = Db::name('commission')->where('uid',$this->auth->id)->page($page,$limit)->select();
        //     $this->success('ok',$list);
        // }else{
        //     $this->error('无法查看提成记录');
        // }
    }

    /**
     * 录入绑定学生手机号
     *
     * @param string $tel 手机号
     * @param string $name 姓名
     * @param string $wechat 微信号
     * 
     * @return void
     */
    public function add_bind($tel,$wechat,$name)
    {
        $this->is_agree();
        $res = Db::name('bind')->where('tel',$tel)->find();
        if($res){
            $this->error('已被绑定过');
        }else{
            $data['name'] = $name;
            $data['wechat'] = $wechat;
            $data['pid'] = $this->auth->id;
            $data['tel'] = $tel;
            Db::name('bind')->insert($data);
            $this->success('录入成功');
        }
    }

    /**
     * 获取订单列表(和提成数据)
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @return void
     */
    public function get_order_list($page = 1,$limit =10)
    {
        $this->is_agree();
        $list = Db::name('order')
                ->where('pid',$this->auth->id)
                ->order('time desc')
                ->page($page,$limit)
                ->select();
        if($list){
            foreach($list as &$val){
                $val['commission'] = Db::name('commission')->where('order_id',$val['id'])->find();
            }
        }
        $this->success('ok',$list);
    }
}